import random


def gen_id_num(region, birthday, sex=1):
    """
    生成身份证号码
    :param region: int 地区代码
    :param birthday: int 出生日期，8位
    :param sex: int 1：男；0：女，默认为男
    :param num: int 生成数量，默认为1
    :return: list 身份证号码
    """
    # 获取随机数后补零
    if sex == 0 or sex == 1:
        while 1:
            order_num = random.randint(0, 999)
            if order_num % 2 == sex:
                break

    id_num = str(region) + str(birthday) + str(order_num).zfill(3)
    id_num = id_num + gen_check_code(id_num)

    return id_num


def gen_check_code(num):
    """
    生成身份证号码最后一位的校验码
    :param num: str 省负责前17为
    :return: str 身份证号码最后一位的校验码
    """
    # 校验码的计算系数
    coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    # 校验码的对应关系
    map1 = {0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2'}
    sum = 0
    i = 0
    for item in coefficient:
        sum += int(num[i]) * item
        i += 1

    # 求11的余数
    remainder = sum % 11

    # 根据校验码的对应关系，找到校验码
    for key, value in map1.items():
        if key == remainder:
            code = value
            break
    return code


if __name__ == '__main__':
    id_num = gen_id_num(110000, 19990303, sex=1)
    print(id_num)
